<?php
    include "../database.php";
    
    //$user_id_addStory = 67;
    $user_id_addStory = $_POST["usr_id_transfer"];
    $queryProject = "SELECT current_project FROM user WHERE id = '$user_id_addStory'";
    $resProject = executeQuery($queryProject);
    $row = mysql_fetch_array($resProject);
    $curr_proj = $row["current_project"];
    
    $story_id = $_POST["story_id"];
    $story_title = urldecode($_POST["title"]);
    $story_text = urldecode($_POST["content"]);
    $story_tests = urldecode($_POST["tests"]);
    $story_priority = $_POST["priority"];
    $story_value = $_POST["value"];
    
    $queryTest = "SELECT * FROM story WHERE story_title = '$story_title' and project_id = '$curr_proj'";
    $res = executeQuery($queryTest);
    $numRows = mysql_num_rows($res);
    $row = mysql_fetch_array($res);
    $ttl = 0;
    $nmr = 0;
    $ttl_missing = 0;
    
    //Zgodbo spremenimo samo, če zgodba s takim imenom še ne obstaja v bazi oziroma ni prazen niz naslov
    if(strlen($story_title)>0){
        $ttl_missing = 1;
        if (($numRows == 0 || ($numRows == 1 && $row['story_id'] == $story_id))){
            // In ce je bussiness value ustrezen.
            $ttl = 1;
            if (is_numeric($story_value) && $story_value > 0 && $story_value < 11){

                $nmr = 1;
                $query = "UPDATE story SET story_title = '$story_title' , story_text = '$story_text', story_test = '$story_tests' , story_priority = '$story_priority' , story_value = '$story_value' WHERE  story_id = '$story_id'";
                $res = executeQuery($query);

                //$msg = message(800);
                //$data = array('bool' => '1', 'success' => '1');
            }
        }
    }
        if ($ttl_missing > 0){
            //$msg = message(999);
            if ($ttl > 0 && $nmr > 0)
                $data = array('bool' => '1', 'success' => '1', 'missing' => '1');
            else if ($ttl > 0 && $nmr < 1)
                $data = array('bool' => '2', 'success' => '0', 'missing' => '1');
            else
                $data = array('bool' => '0', 'success' => '0', 'missing' => '1');
        } else{
            if ($ttl > 0 && $nmr > 0)
                $data = array('bool' => '1', 'success' => '1', 'missing' => '0');
            else if ($ttl > 0 && $nmr < 1)
                $data = array('bool' => '2', 'success' => '0', 'missing' => '0');
            else
                $data = array('bool' => '0', 'success' => '0', 'missing' => '0');
        }

    echo json_encode($data);
?>
